package com.geek.soap;

import lombok.Data;
import lombok.ToString;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.*;
import java.io.ByteArrayInputStream;
import java.util.Iterator;

public class SoapTest {

	/**
	 * 解析soapXML
	 * @param soapXML
	 * @return
	 */
	public static WebserviceResultBean parseSoapMessage(String soapXML) {
		WebserviceResultBean resultBean = new WebserviceResultBean();
		try {
			SOAPMessage msg = formatSoapString(soapXML);
			SOAPBody body = msg.getSOAPBody();
			Iterator<SOAPElement> iterator = body.getChildElements();
			parse(iterator, resultBean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultBean;
	}


	public static void main(String[] args) throws Exception {
		String deptXML = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">";
		deptXML += "<SOAP-ENV:Body>";
		deptXML += "<ns0:GetServiceTest xmlns:ns0=\"http://www.xxx.com/wsdl/test/v003\">";
		deptXML += "<ns1:aa xmlns:ns1=\"http://www.xxx.com/wsdl/test/v003\">";
		deptXML += "<ns1:bb/>";
		deptXML += "<ns1:cc>123456</ns1:cc>";
		deptXML += "<ns1:dd>999</ns1:dd>";
		deptXML += " <ns1:ee>123</ns1:ee>";
		deptXML += "  <ns1:ff>888</ns1:ff>";
		deptXML += "   <ns1:gg>666</ns1:gg>";
		deptXML += "  <ns1:hh>0108A</ns1:hh>";
		deptXML += " </ns1:aa>";
		deptXML += "</ns0:GetServiceTest>";
		deptXML += "</SOAP-ENV:Body>";
		deptXML += "</SOAP-ENV:Envelope>";



		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		dbf.setNamespaceAware(true);
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.newDocument();


//		Marshaller m = jc.createMarshaller();
//
//		m.marshal( element, doc );



		WebserviceResultBean ret = parseSoapMessage(deptXML);

		System.out.println(ret);
		try {
			SOAPMessage msg = formatSoapString(deptXML);
			SOAPBody body = msg.getSOAPBody();
			Iterator<SOAPElement> iterator = body.getChildElements();
			PrintBody(iterator, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * 把soap字符串格式化为SOAPMessage
	 *
	 * @param soapString
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static SOAPMessage formatSoapString(String soapString) {
		MessageFactory msgFactory;
		try {
			msgFactory = MessageFactory.newInstance();
			SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),
					new ByteArrayInputStream(soapString.getBytes("UTF-8")));
			reqMsg.saveChanges();
			return reqMsg;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	/**
	 * 解析soap xml
	 * @param iterator
	 * @param resultBean
	 */
	private static void parse(Iterator<SOAPElement> iterator, WebserviceResultBean resultBean) {
		while (iterator.hasNext()) {
			SOAPElement element = iterator.next();
			if ("ns:BASEINFO".equals(element.getNodeName())) {
				continue;
			} else if ("ns:MESSAGE".equals(element.getNodeName())) {
				Iterator<SOAPElement> it = element.getChildElements();
				SOAPElement el = null;
				while (it.hasNext()) {
					el = it.next();
					if ("RESULT".equals(el.getLocalName())) {
						resultBean.setResult(el.getValue());
						System.out.println("#### " + el.getLocalName() + "  ====  " + el.getValue());
					} else if ("REMARK".equals(el.getLocalName())) {
						resultBean.setRemark(null != el.getValue() ? el.getValue() : "");
						System.out.println("#### " + el.getLocalName() + "  ====  " + el.getValue());
					} else if ("XMLDATA".equals(el.getLocalName())) {
						resultBean.setXmlData(el.getValue());
						System.out.println("#### " + el.getLocalName() + "  ====  " + el.getValue());
					}
				}
			} else if (null == element.getValue()
					&& element.getChildElements().hasNext()) {
				parse(element.getChildElements(), resultBean);
			}
		}
	}


	private static void PrintBody(Iterator<SOAPElement> iterator, String side) {
		while (iterator.hasNext()) {
			Object o=iterator.next();
			if(o!=null)  {
				SOAPElement element=null;
				try{
					element = (SOAPElement) o;
					System.out.println("Node Name:" + element.getNodeName());
					System.out.println("Value:" + element.getValue());
				}catch(Exception e){}
				if ( element !=null ) {
					PrintBody(element.getChildElements(), side + "-----");
				}
			}
		}
	}


	@ToString
	@Data
	public static class WebserviceResultBean{

		private String result;
		private String xmlData;
		private String remark;


	}

}
